﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'entity ContactENI
    /// </summary>
    public abstract class ContactENIOPManager
    {
        /// <summary>
        /// Méthode qui retourne la liste des formateurs
        /// </summary>
        /// <returns>Le liste des formateurs</returns>
        public static List<ContactENI> GetListeFormateurs()
        {
            return GetListeContactENIByCodeDomaine(new List<String> { "PEDA" });
        }

        /// <summary>
        /// Méthode qui retourne une liste de ContactENI par leur codeDomaine
        /// </summary>
        /// <param name="listeCodeDomaine">Liste des codes domaines à filtrer</param>
        /// <returns>La liste des ContactENI</returns>
        public static List<ContactENI> GetListeContactENIByCodeDomaine(List<String> listeCodeDomaine)
        {
            List<ContactENI> listeContact = new List<ContactENI>();

            IQueryable<ContactENI> query = EntityConnection.GetContext().ContactENI;

            // Filtre sur le code domaine
            if (listeCodeDomaine != null && listeCodeDomaine.Count > 0)
            {
                // Ajout d'une expression where ... in (...)
                query = query.Where(EntityExpressionTools.BuildWhereInExpression<ContactENI, String>(it => it.CodeDomaine, listeCodeDomaine));
            }

            query = query.OrderBy(it => it.Identite);

            List<ContactENI> listeContactENI = query.ToList();
            foreach(ContactENI contact in listeContactENI)
            {
                listeContact.Add(contact);
            }

            return listeContact;
        }

        /// <summary>
        /// Méthode de récupération d'un contact eni par son code
        /// </summary>
        /// <param name="codeContact">Le code du contact eni à rechercher</param>
        /// <returns>Le contact eni trouvé</returns>
        public static ContactENI GetContactENIByCode(int codeContactENI) {

            try {
                var query = from it in EntityConnection.GetContext().ContactENI
                            where it.CodeContactENI == codeContactENI
                            select it;

                ContactENI e = null;

                e = query.First();

                return e;

            } catch(Exception ex) {
                throw ex;
            }

        }
    }

}
